#trust institutions
figure_3 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_trust_inst) %>%
  filter(name %in% c("avg_score_trust_inst")) %>%
  mutate(name = case_when(name == "zscore_trust_inst" ~ "Institutions",
                          name == "zscore_trust_ppl" ~ "Civil Society Actors")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("Trust in Politicians\n(Index, 1-10)")

#trust people
figure_4 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_trust_ppl) %>%
  filter(name %in% c("avg_score_trust_ppl")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("Trust in Peers\n(Index, 1-10)")



ggsave(filename = "./outputs/figures/figure_a4a.pdf", plot = figure_3, width = 5, height = 4)
ggsave(filename = "./outputs/figures/figure_a4a.eps", device = "eps", plot = figure_3, width = 5, height = 4)

ggsave(filename = "./outputs/figures/figure_a4b.pdf", plot = figure_4, width = 5, height = 4)
ggsave(filename = "./outputs/figures/figure_a4b.eps", device = "eps", plot = figure_4, width = 5, height = 4)
